package com.talent.controller;

import com.talent.entities.dto.Vacc;
import com.talent.entities.result.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 * @author 周意朗
 * Date: 2022/4/16
 * @since JDK 11
 */
@RestController
@Slf4j
@CrossOrigin
public class VaccListController {
    @Resource
    private RestTemplate restTemplate;

    @Value("${service-url.nacos-user-service}")
    private String serverUrl;

    @GetMapping(value = "/consumer/vacclist/{id}")
    public String getReady(@PathVariable("id") String id,
                           HttpServletRequest request,
                           Model model,
                           HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        return restTemplate.getForObject(serverUrl + "/test/" + id, String.class);
    }

    @PostMapping("/mainpage/getvacclist/{index}")
    public CommonResult<ArrayList> mainPageList(@PathVariable("index") int index,
                                                HttpServletRequest request,
                                                Model model,
                                                HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");

        ArrayList arrayList = restTemplate.postForObject(serverUrl + "/mainpage/" + String.valueOf(index), null, ArrayList.class);

        return new CommonResult<ArrayList>(200, String.valueOf(arrayList.size()), arrayList);
    }

    @PostMapping("/getlistbytype/{type}/{age}/{gender}/{index}")
    public CommonResult<LinkedList> getListByCondition(@PathVariable("index") int index,
                                                       @PathVariable("type") int type,
                                                       @PathVariable("age") int age,
                                                       @PathVariable("gender") int gender,
                                                       HttpServletRequest request,
                                                       Model model,
                                                       HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        MultiValueMap<String, Integer> map = new LinkedMultiValueMap<>(6);
        map.add("index", index);
        map.add("type", type);
        map.add("age", age);
        map.add("gender", gender);
        LinkedList list = restTemplate.postForObject(serverUrl + "/mainpage/getvacclistbycondition", map, LinkedList.class);

        return new CommonResult<LinkedList>(200, "success", list);
    }

    @PostMapping("/getlistbysearch/{input}/{index}")
    public CommonResult<LinkedList> getListBySearch(@PathVariable("index") int index,
                                                    @PathVariable("input") String input,
                                                    HttpServletRequest request,
                                                    Model model,
                                                    HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>(4);
        map.add("index", String.valueOf(index));
        map.add("input", input);
        return new CommonResult<>(200,
                "success",
                restTemplate.postForObject(serverUrl + "/mainpage/getvacclistbysearch", map, LinkedList.class));
    }

    @PostMapping("/getlistbysearchadmin/{input}/{index}")
    public CommonResult<LinkedList> getListBySearchAdmin(@PathVariable("input") String input,
                                                         @PathVariable("index") int index,
                                                         HttpServletRequest request,
                                                         Model model,
                                                         HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        MultiValueMap<String, String> amap = new LinkedMultiValueMap<>(4);
        amap.add("input", input);
        amap.add("index", String.valueOf(index));
        return new CommonResult<>(200,
                "success",
                restTemplate.postForObject(serverUrl + "/mainpage/getvacclistbysearch", amap, LinkedList.class));
    }


    @PostMapping("/getlist/additem")
    public CommonResult<Boolean> addVaccItem(@RequestBody Vacc vacc,
                                             HttpServletRequest request,
                                             Model model,
                                             HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        Boolean flag = restTemplate.postForObject(serverUrl + "/mainpage/getvacclist/addVacc", vacc, Boolean.class);
        if (flag != null && flag) {
            return new CommonResult<Boolean>(200, "success", true);
        }
        return new CommonResult<Boolean>(400, "fail", false);
    }

    @PostMapping("/getlist/update")
    public CommonResult<Boolean> updateVaccItem(@RequestBody Vacc vacc,
                                                HttpServletRequest request,
                                                Model model,
                                                HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        Boolean flag = restTemplate.postForObject(serverUrl + "/mainpage/getvacclist/update", vacc, Boolean.class);
        return new CommonResult<Boolean>(200, "success", flag);
    }

    @PostMapping("/getlist/getvaccbyid/{id}")
    public CommonResult<Vacc> searchById(@PathVariable String id,
                                         HttpServletRequest request,
                                         Model model,
                                         HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        Vacc vacc = restTemplate.postForObject(serverUrl + "/mainpage/getvacclistbyid/", id, Vacc.class);
        return new CommonResult<>(200, "success", vacc);
    }
}
